extends TouchScreenButton

@onready var stick: Sprite2D = $Stick

var stick_center = Vector2.ZERO
var max_length = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	set_process(false)
	stick_center = texture_normal.get_size()/2
	max_length = shape.radius
	position = Vector2(720,720)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	if (get_global_mouse_position()-stick.global_position).length()<800:
		stick.global_position = get_global_mouse_position()
		stick.position = stick_center + (stick.position - stick_center).limit_length(max_length)
	pass

func _input(event):
	#if event is InputEventScreenTouch:
		#if event.pressed:
			#set_process(true)
		#elif event.get_index()==0:
			#set_process(false)
			#stick.position = stick_center
	if event is InputEventScreenDrag:
		if (get_global_mouse_position()-stick.global_position).length()<800:
			set_process(true)
		##elif :
			#set_process(false)
			#stick.position = stick_center
	if event is InputEventScreenTouch:
		if event.get_index()==0:
			set_process(false)
			stick.position = stick_center

func get_joystick_dir()->Vector2:
	var dir = stick.position - stick_center
	return dir.normalized()


func hide_stick():
	stick.hide()


func setting_position(val : Vector2):
	position = val
